package ru.omsu.diveintoandroid.funnycats;

import org.cocos2d.nodes.CCDirector;
import org.cocos2d.opengl.CCGLSurfaceView;
import org.cocos2d.sound.SoundEngine;
import org.gtugs.omsk.funnycats.R;

import ru.omsu.diveintoandroid.funnycats.scenes.GameScene;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

public class GameActivity extends Activity{
	private CCGLSurfaceView mGLSurfaceView;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, 
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

		mGLSurfaceView = new CCGLSurfaceView(this);
		CCDirector director = CCDirector.sharedDirector();
		director.attachInView(mGLSurfaceView);
		director.setDeviceOrientation(CCDirector.kCCDeviceOrientationLandscapeLeft);
		setContentView(mGLSurfaceView);
		
        // show FPS
        CCDirector.sharedDirector().setDisplayFPS(true);
        // frames per second
        CCDirector.sharedDirector().setAnimationInterval(1.0f / 60);
        // Make the Scene active
        CCDirector.sharedDirector().runWithScene(GameScene.scene());
        //
        
        Context context = CCDirector.sharedDirector().getActivity();
        
        SoundEngine.sharedEngine().preloadSound(context, R.raw.sunrise);
        //SoundEngine.sharedEngine().playSound(context, R.raw.sunrise, true);
	}

	@Override
	public void onPause() {
		super.onPause();
		CCDirector.sharedDirector().onPause();
		//SoundEngine.sharedEngine().pauseSound();
	}

	@Override
	public void onResume() {
		super.onResume();
		CCDirector.sharedDirector().onResume();
		//SoundEngine.sharedEngine().resumeSound();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		CCDirector.sharedDirector().end();
	}

	//FIXME: из приложения нельзя выйти по кнопке Back
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return CCDirector.sharedDirector().onKeyDown(event);
	}
}
